package com.example.qjia.model;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Handler;
import android.util.ArrayMap;
import android.util.Log;

import com.example.qjia.Listener.AddHolidayListener;
import com.example.qjia.Listener.LoadTeachersListener;
import com.example.qjia.basic.MyApplication;
import com.example.qjia.data.romotedata.ResponseData;
import com.example.qjia.data.romotedata.Student;
import com.example.qjia.data.romotedata.Teacher;
import com.example.qjia.util.retrofit.RetrofitNet;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Observer;

/**
 * Created by ying_xiang on 2016/8/15.
 */
public class AddHolidayModelImp implements AddHolidayModel {
    @TargetApi(Build.VERSION_CODES.KITKAT)
    @Override
    public void commitHoliday(String BeginDay, String EndDay, String reason,int tid, final AddHolidayListener addHolidayListener) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        Date date = new Date(System.currentTimeMillis());

        String timeApply= format.format(date);
        System.out.println("提交数据："+BeginDay+"--"+EndDay+"--"+timeApply+"--"+reason+"--"+tid);
        final Map<String,String> params;
        params=new HashMap<>();
        System.out.println(MyApplication.getStudentId());
        params.put("sid",MyApplication.getStudentId()+"");
        params.put("tid",tid+"");
        params.put("startTime",BeginDay);
        params.put("endTime",EndDay);
        params.put("applyTime",timeApply);
        params.put("reason",reason);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

                new RetrofitNet().addAbsenceSlip(params,new Observer<ResponseData>() {
                    @Override
                    public void onCompleted() {
                        //完成
                        System.out.println("finish");
                    }

                    @Override
                    public void onError(Throwable e) {
                        //异常
                        System.out.println("error");
                        Log.e("MAIN2",e.getLocalizedMessage()+"--"+e.getMessage());
                    }

                    @Override
                    public void onNext(ResponseData baseBean) {
                        //成功
                        ResponseData  p=baseBean;
                        System.out.println("有返回:");
                        System.out.println("p.getCode()="+p.getCode()+",    p.getMsg()="+p.getMsg());
                        if(p.getCode()==0) {            //表示提交成功


                            System.out.println(p.getCode() + "--" + p.getMsg());
                            addHolidayListener.commitSuccess();
                        }else{
                            addHolidayListener.commitFailed();
                        }

                    }
                });




            }
        }, 1000);
    }

    @Override
    public void loadTeachers(final LoadTeachersListener loadTeachersListener) {//加载teachers
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                List<Teacher> teachers=new ArrayList<Teacher>();
                Teacher t1=new Teacher();
                t1.setTid(1);
                t1.setName("张三");
                Teacher t2=new Teacher();
                t2.setTid(5);
                t2.setName("李四");
                teachers.add(t1);
                teachers.add(t2);
                loadTeachersListener.loadTeachersSuccess(teachers);

            }
        },1000);
    }
}
